﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;

namespace YJKAreaCutter.Utils
{
	public class LinkDocumentOperator
	{
		public static string GetLinkDocPathByStr(Autodesk.Revit.DB.Document doc, string strContains)
		{
			string result = "";
			if (string.IsNullOrEmpty(strContains))
			{
				return result;
			}
			string value = "A";
			string value2 = "S";
			if (strContains.Equals("A"))
			{
				value = "建筑";
				value2 = "建";
			}
			else if (strContains.Equals("S"))
			{
				value = "结构";
				value2 = "结";
			}
            foreach (string text in LinkDocumentOperator.GetLinkDocAbsPaths(doc, (ExternalFileReferenceType)1, 0))
			{
				string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text);
				if (fileNameWithoutExtension.Contains(strContains) || fileNameWithoutExtension.Contains(value) || fileNameWithoutExtension.Contains(value2))
				{
					result = text;
					break;
				}
			}
			return result;
		}

		public static Document GetLinkedDocByCurDoc(Autodesk.Revit.DB.Document docCur, string strKeyStrInPathName)
		{
			string linkDocPathByStr = LinkDocumentOperator.GetLinkDocPathByStr(docCur, strKeyStrInPathName);
			foreach (Autodesk.Revit.DB.Document document in LinkDocumentOperator.GetLinkDocs(docCur))
			{
				if (string.Compare(document.PathName, linkDocPathByStr, false) == 0)
				{
					return document;
				}
			}
			return null;
		}

		public static List<string> GetLinkDocAbsPaths(Autodesk.Revit.DB.Document doc, ExternalFileReferenceType LinkFileType, LinkedFileStatus linkStatus)
		{
			List<string> list = new List<string>();
			foreach (ElementId elementId in ExternalFileUtils.GetAllExternalFileReferences(doc).ToList<ElementId>())
			{
				Element element = doc.GetElement(elementId);
				if (element.IsExternalFileReference())
				{
					ExternalFileReference externalFileReference = element.GetExternalFileReference();
					if (LinkFileType == externalFileReference.ExternalFileReferenceType && linkStatus == externalFileReference.GetLinkedFileStatus())
					{
						string item = ModelPathUtils.ConvertModelPathToUserVisiblePath(externalFileReference.GetAbsolutePath());
						if (!list.Contains(item))
						{
							list.Add(item);
						}
					}
				}
			}
			return list;
		}

		public static bool GetSpecifyLnkDocument(Autodesk.Revit.DB.Document MainDoc, string strContains, out Document outLnkDoc)
		{
			bool result = false;
			outLnkDoc = null;
			string linkDocPathByStr = LinkDocumentOperator.GetLinkDocPathByStr(MainDoc, strContains);
			if (string.IsNullOrEmpty(linkDocPathByStr))
			{
				return result;
			}
			foreach (Autodesk.Revit.DB.Document document in LinkDocumentOperator.GetLinkDocs(MainDoc))
			{
				if (document.PathName.Equals(linkDocPathByStr))
				{
					result = true;
					outLnkDoc = document;
					break;
				}
			}
			return result;
		}

		public static List<Document> GetLinkDocs(Autodesk.Revit.DB.Document doc)
		{
			List<Document> list = new List<Document>();
			try
			{
				foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(RevitLinkInstance)))
				{
					RevitLinkInstance revitLinkInstance = (RevitLinkInstance)element;
					if (revitLinkInstance.GetLinkDocument() != null)
					{
						list.Add(revitLinkInstance.GetLinkDocument());
					}
				}
			}
			catch
			{
			}
			return list;
		}
	}
}
